package com.superteam.zzl.activity.RecommandActivity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.superteam.zzl.activity.BaseActivity;
import com.superteam.zzl.activity.MainActivity;
import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.databinding.ActivityBanner1Binding;
import com.superteam.zzl.utils.ShareUtils;

public class Banner1Activity extends BaseActivity {

    public String link;
    private ActivityBanner1Binding banner1Binding;
    private WebView webView;
    private Toolbar tb;
    private ListView lv;
    private TextView tv;
    private ProgressBar progressBar;

    @Override
    public void widgetClick(View v) {
    }

    @Override
    public ViewDataBinding initDataBinding() {
        banner1Binding = DataBindingUtil.setContentView(this,R.layout.activity_banner1);
        tv = banner1Binding.tvTb;
        webView = banner1Binding.webView;
        progressBar = banner1Binding.pbLoad;
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
            }
        });
        //WebView的设置对象
        WebSettings setting=webView.getSettings();
        //是否支持js
        setting.setJavaScriptEnabled(true);
        //设置自适应
        setting.setUseWideViewPort(true);
        webView.setNetworkAvailable(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口，如window.open()，默认为false
        webView.getSettings().setJavaScriptEnabled(true);//是否允许执行js，默认为false。设置true时，会提醒可能造成XSS漏洞
        webView.getSettings().setSupportZoom(false);//是否可以缩放，默认true
        webView.getSettings().setBuiltInZoomControls(false);//是否显示缩放按钮，默认false
        webView.getSettings().setUseWideViewPort(true);//设置此属性，可任意比例缩放。大视图模式
        webView.getSettings().setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        webView.getSettings().setAppCacheEnabled(true);//是否使用缓存
        webView.getSettings().setDomStorageEnabled(true);//DOM Storage

        tb = banner1Binding.tb;
        tb.inflateMenu(R.menu.rectbmenu);
        tb.setNavigationIcon(R.drawable.back);
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                Intent intent = getIntent();
                setResult(200,intent);
            }
        });
        tb.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                //setDialog();
                ShareUtils.shareAll(Banner1Activity.this);
                return true;
            }
        });
        return banner1Binding;
    }

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void doBusiness(Context mContext) {
        Intent intent = getIntent();
        if(intent.getStringExtra("Flag").equals("Rec")||intent.getStringExtra("Flag").equals("Store")) {
            tb.setVisibility(View.GONE);
        }else{
            tb.setVisibility(View.VISIBLE);
        }
        link = intent.getStringExtra("Link");
//        Log.e("data",webView.getTag()+"\n"+webView.getProgress()+"");
//        tb.setTitle(webView.getTitle());
        tv.setText(intent.getStringExtra("title"));
        webView.loadUrl(link);

    }

    private void setDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(this,R.layout.activity_banner1_dialog,null);
        lv = (ListView) view.findViewById(R.id.lv_banner);
        builder.setView(view);
        builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
//        View v = lv.getChildAt(lv.getChildCount()-1);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("data","条目"+position);
            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode)
        {
            case KeyEvent.KEYCODE_BACK://回退键
                //验证WebView是否可以回退
                if(webView.canGoBack()){
                    webView.goBack();
                    return true;
                }
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
